ajax = 
{
    connection_fail: 
        function ()
        {
            return "Unable to complete request";
        },
        
    please_wait_text: 
        function ()
        {
            return '<img border=0 align="absmiddle" src="img/wait.gif"" />Please wait ...';
        },    
        
    /**
     * Creates a cross-browser XMLHttpRequest 
     */
    create :
        function()
        {
            // Kontrollojme browsers
            if (window.XMLHttpRequest) 
            {
                try
                {
                    return new XMLHttpRequest();
                }
                catch(e)
                {
                    return false;
                }
            }
            else 
                // Ne rast se kemi IE
                if (window.ActiveXObject) 
                {   
                    try 
                    {
                        return new ActiveXObject ("Microsoft.XMLHTTP");
                    }
                    catch (e) 
                    { 
                        // Kthejme false ne rast deshtimi
                        return false;
                    }
                }
        },

    /**
     * send a get request to the url defined in 'url' parameter
     * 
     * callback is the function to call when finished 
     */
        
    get: function(url, callback)
    {
    	$.ajax({
    		'url': url,
    		'success': function(e){
    			callback(e);
    		},
    		'error': function(e){
    			callback(false);
    		}
    	});
    },
        
        
    /**
     * send a post request to 'url' with post parameters 'params'
     * 
     * callback is the function to call when request is finished 
     */
    
    post: function(url, params, callback){
    	$.ajax({
    		'url': url,
    		'method': 'POST',
    		'data': params,
    		'success': function(e){
    			callback(e);
    		},
    		'error': function(e){
    			callback(false);
    		}
    	});
    },
    /**
     * sends a get request to the url in the parameter
     * and alerts the result 
     */
    echoRequest :
        function(url)
        {
            ajax.get(url,
                function (result)
                {
                    if (result)
                        alert(result);
                    else
                        alert(ajax.connection_fail());
                })
        },
        
        
    /**
     * issues a get request.
     * 
     * sets the html of 'eleId' to an image + Please wait text
     * while it is still loading.
     * 
     * then the html is set to that of the request response
     * 
     * if failed will restore the html it previously had 
     * 
     * appendStr is a string that can be appended to the end of
     * the response text
     */
    htmlFromRequest :
        function(eleId, fromUrl, appendStr)
        {
            oldHtml = getHTML(eleId);
            setHTML(eleId, ajax.please_wait_text());
            ajax.get(fromUrl,
                function(result)
                {
                    if (result)
                    {
                        if (appendStr != undefined)
                            result += appendStr;
                        
                        setHTML(eleId, result);               
                    }
                    else
                    {
                        alert(ajax.connection_fail());
                        setHTML(eleId, oldHtml);
                    }
                });
        },
        
    /**
     * same as htmlFromRequest 
     */
    htmlFromPostRequest :
        function(eleId, fromUrl, params, appendStr)
        {
            oldHtml = getHTML(eleId);
            setHTML(eleId, ajax.please_wait_text());
            
            ajax.post(fromUrl, params,
                function(result)
                {
                    if (result)
                    {
                        if (appendStr != undefined)
                            result += appendStr;
                            
                        document.write(result);
                        return
                        
                        setHTML(eleId, result);               
                    }
                    else
                    {
                        alert(ajax.connection_fail());
                        setHTML(eleId, oldHtml);
                    }
                });
        }  
};

function setHTML(id, newHtml)
{
    document.getElementById(id).innerHTML = newHtml;
}

function getHTML(id, newHtml)
{
    return document.getElementById(id).innerHTML;
}